//
//  AVRequest+Relation.m
//  项目三
//
//  Created by rimi1 on 2016/12/10.
//  Copyright © 2016年 Wangxingqian. All rights reserved.
//

#import "AVRequest+Relation.h"

@implementation AVRequest (Relation)

+ (void)avCollcetWithObj:(AVObject *)obj query:(QueryArr)queryBlock {
    AVRelation *relation = [obj relationForKey:@"collectList"];
    AVQuery *query = [relation query];
    [query includeKey:@"dynamic"];
    [query includeKey:@"dynamic.user"];
    [AVRequest avQueryWithQuery:query queryArr:^(NSArray *objects, NSError *error) {
        queryBlock(objects, error);
    }];
}

+ (void)avDynamicWithObj:(AVObject *)obj query:(QueryArr)queryBlock {
    AVRelation *relation = [obj relationForKey:@"myDynamic"];
    AVQuery *query = [relation query];
    [query includeKey:@"user"];
    [AVRequest avQueryWithQuery:query queryArr:^(NSArray *objects, NSError *error) {
        queryBlock(objects, error);
    }];
}

@end

@implementation AVRequest (Collect)

+ (void)avIsCollectWithMenuId:(NSString *)menuId query:(QueryArr)queryBlock {
    AVObject *obj = [AVObject objectWithClassName:@"MyUser" objectId:[MyUser sharedUser].userId];
    AVRelation *relation = [obj relationForKey:@"collectList"];
    AVQuery *query = [relation query];
    [query whereKey:@"id" equalTo:menuId];
    [AVRequest avQueryWithQuery:query queryArr:^(NSArray *objects, NSError *error) {
        if (query) {
            queryBlock(objects, error);
        }
    }];
}

+ (void)avIsPersonalCollectWithObj:(AVObject *)obj objArr:(QueryArr)objArr {
    AVObject *user = [AVObject objectWithClassName:@"MyUser" objectId:[MyUser sharedUser].userId];
    AVRelation *relation = [user relationForKey:@"collectList"];
    AVQuery *query = [relation query];
    [query whereKey:@"dynamic" equalTo:obj];
    [AVRequest avQueryWithQuery:query queryArr:^(NSArray *objects, NSError *error) {
        if (objArr) {
            objArr(objects, error);
        }
    }];
}
// 删除系统收藏
+ (void)avRemoveCollectWithMenuId:(NSString *)menuId removeSucceed:(AVSucceed)removeSucceed {
    AVObject *userObj = [AVObject objectWithClassName:@"MyUser" objectId:[MyUser sharedUser].userId];
    AVRelation *relation = [userObj relationForKey:@"collectList"];
    AVQuery *query = [relation query];
    [query whereKey:@"id" equalTo:menuId];
    // 查询出Obj
    [AVRequest avQueryWithQuery:query queryArr:^(NSArray *objects, NSError *error) {
        if (error) {
            removeSucceed(NO, error);
            return;
        }
        // 删除obj
        [AVRequest avSaveAllRemoveWithObj:userObj key:@"collectList" objArr:@[objects.firstObject] rSucceed:^(BOOL succeed, NSError *error) {
            removeSucceed(succeed, error);
        }];
    }];
}
// 删除用户收藏
+ (void)avRemoveCollectWithObj:(AVObject *)obj removeSucceed:(AVSucceed)removeSucceed {
    AVObject *userObj = [AVObject objectWithClassName:@"MyUser" objectId:[MyUser sharedUser].userId];
    AVRelation *relation = [userObj relationForKey:@"collectList"];
    AVQuery *query = [relation query];
    [query whereKey:@"dynamic" equalTo:obj];
    [AVRequest avQueryWithQuery:query queryArr:^(NSArray *objects, NSError *error) {
        // 删除obj
        [AVRequest avSaveAllRemoveWithObj:userObj key:@"collectList" objArr:@[objects.firstObject] rSucceed:^(BOOL succeed, NSError *error) {
            if (removeSucceed) {
                removeSucceed(succeed, error);
            }
        }];
    }];
}

+ (void)avAddCollectWithDynamicObj:(AVObject *)dynamicObj menuId:(NSString *)menuId name:(NSString *)name food:(NSString *)food img:(NSString *)img addSucceed:(AVSucceed)addSucceed {
    AVObject *obj = [AVObject objectWithClassName:@"Collect"];
    [obj setObject:dynamicObj forKey:@"dynamic"];
    [obj setObject:menuId forKey:@"id"];
    [obj setObject:name forKey:@"name"];
    [obj setObject:food forKey:@"food"];
    [obj setObject:img forKey:@"img"];
    
    [AVRequest avAddRelationWithObj:obj key:@"collectList" succeed:^(BOOL succeed, NSError *error) {
        if (addSucceed) {
            addSucceed(succeed, error);
        }
    }];
}

+ (void)avEditWithName:(NSString *)name food:(NSString *)food step:(NSString *)step img:(NSData *)img sueeccd:(AVSucceed)editSueeccd {
    AVObject *obj = [AVObject objectWithClassName:@"Dynamic"];
    [obj setObject:name forKey:@"name"];
    [obj setObject:food forKey:@"food"];
    [obj setObject:step forKey:@"step"];
    // 关联用户
    AVObject *user = [AVObject objectWithClassName:@"MyUser" objectId:[MyUser sharedUser].userId];
    NSString *str = [NSString stringWithFormat:@"%ld", [[MyUser sharedUser].myDynamicCount integerValue] + 1];
    [user setObject:str forKey:@"myDynamicCount"];
    [obj setObject:user forKey:@"user"];
    
    [AVRequest avImageWithData:img succeed:^(NSString *url, BOOL succeed, NSError *error) {
        if (error) {
            if (editSueeccd) {
                editSueeccd(succeed, error);
            }
            return;
        }
        
        [obj setObject:url forKey:@"img"];
        [AVRequest avAddRelationWithObj:obj key:@"myDynamic" succeed:^(BOOL succeed, NSError *error) {
            editSueeccd(succeed, error);
        }];
        
    }];
}

@end

@implementation AVRequest (Dynamic)

+ (void)avDynamicWithPage:(NSInteger)page Query:(QueryArr)queryBlock {
    AVQuery *query = [AVQuery queryWithClassName:@"Dynamic"];
    [query includeKey:@"user"];
    NSDate *date = [NSDate date];
    [query whereKey:@"createdAt" lessThanOrEqualTo:date];
    
    CGFloat rows = 10;
    query.limit = rows;
    query.skip = rows * (page - 1);
    [AVRequest avQueryWithQuery:query queryArr:^(NSArray *objects, NSError *error) {
        queryBlock(objects, error);
    }];
}

+ (void)avConcernDynamicWithPage:(NSInteger)page Query:(QueryArr)queryBlock {
    AVObject *user = [AVObject objectWithClassName:@"MyUser" objectId:[MyUser sharedUser].userId];
    AVQuery *concern = [[user relationForKey:@"concern"] query];
    
    [AVRequest avQueryWithQuery:concern queryArr:^(NSArray *objects, NSError *error) {
        if (error) {
            queryBlock(@[], error);
            return;
        }
        AVQuery *query = [AVQuery queryWithClassName:@"Dynamic"];
        NSDate *date = [NSDate date];
        [query includeKey:@"user"];
        [query whereKey:@"createdAt" lessThanOrEqualTo:date];
        // 是否包含关注
        [query whereKey:@"user" containedIn:objects];
        
        CGFloat rows = 10;
        query.limit = rows;
        query.skip = rows * (page - 1);
        
        [AVRequest avQueryWithQuery:query queryArr:^(NSArray *objects, NSError *error) {
            queryBlock(objects, error);
        }];
    }];
}


@end














